package br.edu.ufcg.msnlab2.grupo03;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import br.edu.ufcg.msnlab2.MSNLabException;
import br.edu.ufcg.msnlab2.grupo03.romberg.RombergMethod;
import br.edu.ufcg.msnlab2.misc.Function;
import br.edu.ufcg.msnlab2.misc.FunctionImpl;

public class RombergMethodTestCase {

	private Function identity;
	private Function constant;

	@Before
	public void setUp() throws Exception {
		this.identity = new FunctionImpl("x");
		this.constant = new FunctionImpl("1");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testConstructorMissingFunction() {
		try {
			new RombergMethod(null, 0, 1);
			fail("Exception should raise for missing function");
		} catch (MSNLabException e) {
		}
	}

	@Test
	public void testConstructorWrongLimitOrder() {
		try {
			new RombergMethod(identity, 1, 0);
			fail("Exception should raise for wrong limit order");
		} catch (MSNLabException e) {
		}
		try {
			new RombergMethod(identity, 1, -1);
			fail("Exception should raise for wrong limit order");
		} catch (MSNLabException e) {
		}
	}

	@Test
	public void testConstructor() throws MSNLabException {
		new RombergMethod(identity, 0, 1);
		new RombergMethod(identity, 0, 1000);
		new RombergMethod(identity, 0, 0);
		new RombergMethod(identity, -1, 1);
	}

	@Test
	public void testZeroIterationsIntegration() throws MSNLabException {
		assertEquals(0, RombergMethod.execute(identity, 1, 2, 0, 0), 0);
		assertEquals(0, RombergMethod.execute(constant, 0, 0, 0, 0), 0);
		assertEquals(0, RombergMethod.execute(identity, 0, 0, 5000, 0), 0);
	}

	@Test
	public void testIndentityFunctionIntegration() throws MSNLabException {
		assertEquals(0.5, RombergMethod.execute(identity, 0, 1, 5, 0), 0);
		assertEquals(0.5, RombergMethod.execute(identity, 0, 1, 100, 0), 0);
		assertEquals(50, RombergMethod.execute(identity, 0, 10, 200, 0), 0);
	}
	
	@Test
	public void testConstantIntegration() throws MSNLabException {
		assertEquals(1, RombergMethod.execute(constant, 0, 1, 5, 0), 0);
		assertEquals(1, RombergMethod.execute(constant, 0, 1, 100, 0), 0);
		assertEquals(10, RombergMethod.execute(constant, 0, 10, 200, 0), 0);
		assertEquals(50, RombergMethod.execute(constant, -25, 25, 10, 0), 0);
	}

}
